﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using DIS_DAO.DTO;
using DIS_DAO;
using DIS_DAO.Helpers;
using System.Web.Hosting;
using System.IO;

namespace DIS_WS_BusinessLayer
{
    /// <summary>
    /// The class is used to execute businesses
    /// </summary>
    public class DIS_Service_BusinessLayer : IDIS_Service_BusinessLayer
    {
        #region Variables

        private IDataProvider _provider;
        
        #endregion

        #region Constructors

        public DIS_Service_BusinessLayer()
		{
            _provider = new DataProvider(DIS_WS_BusinessLayer.Helpers.GlobalVariables.DbConnectionString);
		}

        #endregion

        #region CATEGORY

        public List<CategoryDTO> Category_SelectAll()
		{
			return _provider.Category_SelectAll();
		}
		public CategoryDTO Category_SelectByID(int id)
		{
			return _provider.Category_SelectByID(id);
		}
		public bool Category_Insert(CategoryDTO obj)
		{
			return _provider.Category_Insert(obj);
		}
		public bool Category_Delete(int id)
		{
			return _provider.Category_Delete(id);
		}
		public bool Category_Update(CategoryDTO obj)
		{
			return _provider.Category_Update(obj);
		}

        #endregion

        #region DAILYREPORT

        public List<DailyReportDTO> DailyReport_SelectAll()
		{
			return _provider.DailyReport_SelectAll();
		}
		public DailyReportDTO DailyReport_SelectByID(int id)
		{
			return _provider.DailyReport_SelectByID(id);
		}
		public bool DailyReport_Insert(DailyReportDTO obj)
		{
			return _provider.DailyReport_Insert(obj);
		}
		public bool DailyReport_Delete(int id)
		{
			return _provider.DailyReport_Delete(id);
		}
		public bool DailyReport_Update(DailyReportDTO obj)
		{
			return _provider.DailyReport_Update(obj);
		}
        public List<DailyReportDTO> DailyReport_SelectByIDChemistWareHouse(int idChemistWareHouse)
		{
			return _provider.DailyReport_SelectByIDChemistWareHouse(idChemistWareHouse);
		}
        public DailyReportDTO DailyReport_SelectByIDChemistWareHouseAndReportDate(int idChemistWareHouse, DateTime reportdate)
        {
            return _provider.DailyReport_SelectByIDChemistWareHouseAndReportDate(idChemistWareHouse, reportdate);
        }

        #endregion

        #region WEEKLYREPORT

        public List<WeeklyReportDTO> WeeklyReport_SelectAll()
		{
			return _provider.WeeklyReport_SelectAll();
		}
		public WeeklyReportDTO WeeklyReport_SelectByID(int id)
		{
			return _provider.WeeklyReport_SelectByID(id);
		}
		public bool WeeklyReport_Insert(WeeklyReportDTO obj)
		{
			return _provider.WeeklyReport_Insert(obj);
		}
		public bool WeeklyReport_Delete(int id)
		{
			return _provider.WeeklyReport_Delete(id);
		}
		public bool WeeklyReport_Update(WeeklyReportDTO obj)
		{
			return _provider.WeeklyReport_Update(obj);
		}
        public List<WeeklyReportDTO> WeeklyReport_SelectByIDChemistWareHouse(int idChemistWareHouse)
		{
			return _provider.WeeklyReport_SelectByIDChemistWareHouse(idChemistWareHouse);
		}
        public WeeklyReportDTO WeeklyReport_SelectByIDChemistWareHouseAndReportDate(int idChemistWareHouse, DateTime startdate, DateTime enddate)
        {
            return _provider.WeeklyReport_SelectByIDChemistWareHouseAndReportDate(idChemistWareHouse, startdate, enddate);
        }

        #endregion

        #region ITEM

        public List<ItemDTO> Item_SelectAll()
		{
			return _provider.Item_SelectAll();
		}
		public ItemDTO Item_SelectByID(int id)
		{
			return _provider.Item_SelectByID(id);
		}
		public bool Item_Insert(ItemDTO obj)
		{
			return _provider.Item_Insert(obj);
		}
		public bool Item_Delete(int id)
		{
			return _provider.Item_Delete(id);
		}
		public bool Item_Update(ItemDTO obj)
		{
			return _provider.Item_Update(obj);
		}
        public List<ItemDTO> Item_SelectByIDCategory(int idcategory)
		{
			return _provider.Item_SelectByIDCategory(idcategory);
		}

        public void DeleteFile(string filename)
        {
            try
            {
                FileInfo file = new FileInfo(filename);
                if (file.Exists)
                {
                    file.Delete();
                }
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                {
                    GlobalVariables.Logger.Error(ex.InnerException.Message);
                }
                else
                {
                    GlobalVariables.Logger.Error(ex.Message);
                }
            }
        }

        public bool Item_UploadFile(string fileName, byte[] buffer, string oldname = "")
        {
            GlobalVariables.Logger.Info(fileName);
            string basePath = HostingEnvironment.ApplicationPhysicalPath + @"\Images\";
            string apPath = basePath + fileName;
            GlobalVariables.Logger.Info(apPath);

            if (oldname != "" && oldname != "noimages.png")
            {
                DeleteFile(basePath + oldname);
            }

            try
            {
                using (FileStream fs = new FileStream(apPath, FileMode.OpenOrCreate, FileAccess.Write))
                {
                    fs.Write(buffer, 0, (int)buffer.Length);
                    fs.Close();
                    fs.Dispose();
                }
                return true;
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                {
                    GlobalVariables.Logger.Error(ex.InnerException.Message);
                }
                else
                {
                    GlobalVariables.Logger.Error(ex.Message);
                }
            }
            return false;
        }

        public byte[] Item_DownloadFile(string fileName)
        {
            string apPath = HostingEnvironment.ApplicationPhysicalPath + @"\Images\" + fileName;
            byte[] img = null;

            try
            {
                FileStream fs = new FileStream(apPath, FileMode.OpenOrCreate, FileAccess.Read);
                img = new byte[fs.Length];
                fs.Read(img, 0, Convert.ToInt32(fs.Length));
                fs.Close();
                fs.Dispose();
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                {
                    GlobalVariables.Logger.Error(ex.InnerException.Message);
                }
                else
                {
                    GlobalVariables.Logger.Error(ex.Message);
                }
            }
            return img;
        }

        #endregion

        #region EMPLOYEE

        public List<EmployeeDTO> Employee_SelectAll()
		{
			return _provider.Employee_SelectAll();
		}
		public EmployeeDTO Employee_SelectByID(int id)
		{
			return _provider.Employee_SelectByID(id);
		}
		public bool Employee_Insert(EmployeeDTO obj)
		{
			return _provider.Employee_Insert(obj);
		}
		public bool Employee_Delete(int id)
		{
			return _provider.Employee_Delete(id);
		}
		public bool Employee_Update(EmployeeDTO obj)
		{
			return _provider.Employee_Update(obj);
		}
        public List<EmployeeDTO> Employee_SelectByIDRole(int idrole)
		{
			return _provider.Employee_SelectByIDRole(idrole);
		}
        public List<EmployeeDTO> Employee_SelectByIDChemistWareHouse(int idChemistWareHouse)
		{
			return _provider.Employee_SelectByIDChemistWareHouse(idChemistWareHouse);
		}
        public EmployeeDTO Employee_SelectByUserName(string username)
        {
            return _provider.Employee_SelectByUserName(username);
        }
        public EmployeeDTO Employee_SelectByUserNameAndPassword(string username, string password)
        {
            return _provider.Employee_SelectByUserNameAndPassword(username, password);
        }

        #endregion

        #region NATIONALPROMOTION

        public List<NationalPromotionDTO> NationalPromotion_SelectAll()
		{
			return _provider.NationalPromotion_SelectAll();
		}
		public NationalPromotionDTO NationalPromotion_SelectByID(int id)
		{
			return _provider.NationalPromotion_SelectByID(id);
		}
		public bool NationalPromotion_Insert(NationalPromotionDTO obj)
		{
			return _provider.NationalPromotion_Insert(obj);
		}
		public bool NationalPromotion_Delete(int id)
		{
			return _provider.NationalPromotion_Delete(id);
		}
		public bool NationalPromotion_Update(NationalPromotionDTO obj)
		{
			return _provider.NationalPromotion_Update(obj);
		}
		public NationalPromotionDTO NationalPromotion_SelectByIDItem(int iditem)
		{
			return _provider.NationalPromotion_SelectByIDItem(iditem);
		}

        #endregion

        #region LOCALPROMOTION

        public List<LocalPromotionDTO> LocalPromotion_SelectAll()
		{
			return _provider.LocalPromotion_SelectAll();
		}
		public LocalPromotionDTO LocalPromotion_SelectByID(int id)
		{
			return _provider.LocalPromotion_SelectByID(id);
		}
		public bool LocalPromotion_Insert(LocalPromotionDTO obj)
		{
			return _provider.LocalPromotion_Insert(obj);
		}
		public bool LocalPromotion_Delete(int id)
		{
			return _provider.LocalPromotion_Delete(id);
		}
        public bool LocalPromotion_DeleteByIDItemAndIDChemistWareHouse(int iditem, int idChemistWareHouse)
        {
            return _provider.LocalPromotion_DeleteByIDItemAndIDChemistWareHouse(iditem, idChemistWareHouse);
        }
		public bool LocalPromotion_Update(LocalPromotionDTO obj)
		{
			return _provider.LocalPromotion_Update(obj);
		}
        public List<LocalPromotionDTO> LocalPromotion_SelectByIDItem(int iditem)
		{
			return _provider.LocalPromotion_SelectByIDItem(iditem);
		}
        public List<LocalPromotionDTO> LocalPromotion_SelectByIDChemistWareHouse(int idChemistWareHouse)
		{
			return _provider.LocalPromotion_SelectByIDChemistWareHouse(idChemistWareHouse);
		}
        public LocalPromotionDTO LocalPromotion_SelectByIDItemAndIDChemistWareHouse(int iditem, int idChemistWareHouse)
        {
            return _provider.LocalPromotion_SelectByIDItemAndIDChemistWareHouse(iditem, idChemistWareHouse);
        }

        #endregion

        #region ORDERS

        public List<OrdersDTO> Orders_SelectAll()
		{
			return _provider.Orders_SelectAll();
		}
		public OrdersDTO Orders_SelectByID(int id)
		{
			return _provider.Orders_SelectByID(id);
		}
		public bool Orders_Insert(ref OrdersDTO obj)
		{
			return _provider.Orders_Insert(ref obj);
		}
		public bool Orders_Delete(int id)
		{
			return _provider.Orders_Delete(id);
		}
		public bool Orders_Update(OrdersDTO obj)
		{
			return _provider.Orders_Update(obj);
		}
        public List<OrdersDTO> Orders_SelectByIDChemistWareHouse(int idChemistWareHouse)
		{
            return _provider.Orders_SelectByIDChemistWareHouse(idChemistWareHouse);
		}
        public List<OrdersDTO> Orders_SelectByIDChemistWareHouseAndOrderDate(int idChemistWareHouse, DateTime orderdate)
        {
            return _provider.Orders_SelectByIDChemistWareHouseAndOrderDate(idChemistWareHouse, orderdate);
        }

        public List<OrdersDTO> Orders_SelectByIDCustomer(int idcustomer)
        {
            return _provider.Orders_SelectByIDCustomer(idcustomer);
        }

        #endregion

        #region ORDERDETAIL

        public List<OrderDetailDTO> OrderDetail_SelectAll()
		{
			return _provider.OrderDetail_SelectAll();
		}
		public OrderDetailDTO OrderDetail_SelectByID(int id)
		{
			return _provider.OrderDetail_SelectByID(id);
		}
		public bool OrderDetail_Insert(OrderDetailDTO obj)
		{
			return _provider.OrderDetail_Insert(obj);
		}
		public bool OrderDetail_Delete(int id)
		{
			return _provider.OrderDetail_Delete(id);
		}
        public bool OrderDetail_DeleteByOrder(int idorder)
        {
            return _provider.OrderDetail_DeleteByOrder(idorder);
        }
		public bool OrderDetail_Update(OrderDetailDTO obj)
		{
			return _provider.OrderDetail_Update(obj);
		}
        public List<OrderDetailDTO> OrderDetail_SelectByIDOrder(int idorder)
		{
			return _provider.OrderDetail_SelectByIDOrder(idorder);
		}
        public List<OrderDetailDTO> OrderDetail_SelectByIDItem(int iditem)
		{
			return _provider.OrderDetail_SelectByIDItem(iditem);
		}

        #endregion

        #region ROLEFUNCTIONS

        public List<RoleFunctionsDTO> RoleFunctions_SelectAll()
		{
			return _provider.RoleFunctions_SelectAll();
		}
		public RoleFunctionsDTO RoleFunctions_SelectByID(int id)
		{
			return _provider.RoleFunctions_SelectByID(id);
		}
		public bool RoleFunctions_Insert(RoleFunctionsDTO obj)
		{
			return _provider.RoleFunctions_Insert(obj);
		}
		public bool RoleFunctions_Delete(int id)
		{
			return _provider.RoleFunctions_Delete(id);
		}
		public bool RoleFunctions_Update(RoleFunctionsDTO obj)
		{
			return _provider.RoleFunctions_Update(obj);
		}
        public List<RoleFunctionsDTO> RoleFunctions_SelectByIDRole(int idrole)
		{
			return _provider.RoleFunctions_SelectByIDRole(idrole);
		}
        public List<RoleFunctionsDTO> RoleFunctions_SelectByIDFunction(int idfunction)
		{
			return _provider.RoleFunctions_SelectByIDFunction(idfunction);
		}

        #endregion

        #region FUNCTIONS

        public List<FunctionsDTO> Functions_SelectAll()
		{
			return _provider.Functions_SelectAll();
		}
		public FunctionsDTO Functions_SelectByID(int id)
		{
			return _provider.Functions_SelectByID(id);
		}
		public bool Functions_Insert(FunctionsDTO obj)
		{
			return _provider.Functions_Insert(obj);
		}
		public bool Functions_Delete(int id)
		{
			return _provider.Functions_Delete(id);
		}
		public bool Functions_Update(FunctionsDTO obj)
		{
			return _provider.Functions_Update(obj);
		}

        #endregion

        #region ROLE

        public List<RoleDTO> Role_SelectAll()
		{
			return _provider.Role_SelectAll();
		}
		public RoleDTO Role_SelectByID(int id)
		{
			return _provider.Role_SelectByID(id);
		}
		public bool Role_Insert(RoleDTO obj)
		{
			return _provider.Role_Insert(obj);
		}
		public bool Role_Delete(int id)
		{
			return _provider.Role_Delete(id);
		}
		public bool Role_Update(RoleDTO obj)
		{
			return _provider.Role_Update(obj);
		}

        #endregion

        #region CHEMIST WAREHOUSE

        public List<ChemistWareHouseDTO> ChemistWareHouse_SelectAll()
		{
			return _provider.ChemistWareHouse_SelectAll();
		}
		public ChemistWareHouseDTO ChemistWareHouse_SelectByID(int id)
		{
			return _provider.ChemistWareHouse_SelectByID(id);
		}
		public bool ChemistWareHouse_Insert(ChemistWareHouseDTO obj)
		{
			return _provider.ChemistWareHouse_Insert(obj);
		}
		public bool ChemistWareHouse_Delete(int id)
		{
			return _provider.ChemistWareHouse_Delete(id);
		}
		public bool ChemistWareHouse_Update(ChemistWareHouseDTO obj)
		{
			return _provider.ChemistWareHouse_Update(obj);
		}

        #endregion

        #region CUSTOMER

        public List<CustomerDTO> Customer_SelectAll()
        {
            return _provider.Customer_SelectAll();
        }
        public CustomerDTO Customer_SelectByID(int id)
        {
            return _provider.Customer_SelectByID(id);
        }
        public bool Customer_Insert(CustomerDTO obj)
        {
            return _provider.Customer_Insert(obj);
        }
        public bool Customer_Delete(int id)
        {
            return _provider.Customer_Delete(id);
        }
        public bool Customer_Update(CustomerDTO obj)
        {
            return _provider.Customer_Update(obj);
        }

        public CustomerDTO Customer_SelectByUserName(string username)
        {
            return _provider.Customer_SelectByUserName(username);
        }

        public CustomerDTO Customer_SelectByUserNameAndPassword(string username, string password)
        {
            return _provider.Customer_SelectByUserNameAndPassword(username, password);
        }

        #endregion

        #region CART

        public List<CartDTO> Cart_SelectByCartID(string cartid)
        {
            return _provider.Cart_SelectByCartID(cartid);
        }
        public CartDTO Cart_SelectByIDAndRecordID(string cartid, int recordid)
        {
            return _provider.Cart_SelectByIDAndRecordID(cartid, recordid);
        }
        public CartDTO Cart_SelectByRecordID(int recordid)
        {
            return _provider.Cart_SelectByRecordID(recordid);
        }
        public CartDTO Cart_SelectByIDAndItem(string cartid, int itemid)
        {
            return _provider.Cart_SelectByIDAndItem(cartid, itemid);
        }
        public bool Cart_Insert(CartDTO obj)
        {
            return _provider.Cart_Insert(obj);
        }
        public bool Cart_Delete(string cartid, int recordid)
        {
            return _provider.Cart_Delete(cartid, recordid);
        }
        public bool Cart_Empty(string cartid)
        {
            return _provider.Cart_Empty(cartid);
        }
        public bool Cart_Update(CartDTO obj)
        {
            return _provider.Cart_Update(obj);
        }

        #endregion
    }
}
